package com.pktechan.manage.service.impl;


import com.pktechan.manage.base.BaseMapper;
import com.pktechan.manage.base.BaseServiceImpl;
import com.pktechan.manage.dao.SysUserMapper;
import com.pktechan.manage.entity.SysUser;
import com.pktechan.manage.service.LoginService;
import com.pktechan.manage.utils.EncrptUtil;
import com.pktechan.manage.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <br>
 * <b>功能：</b>LoginService<br>
 * <b>作者：</b>Wang Jiahao<br>
 * <b>日期：</b> Mar 23, 2016 <br>
 * <b>版权所有：<b>版权所有(C) 2016，高晟财富<br>
 */
@Service("loginService")
public class LoginServiceImpl extends BaseServiceImpl<SysUser> implements LoginService {
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginService.class);

	@Autowired
	private SysUserMapper sysUserMapper;

	/**
	 * 根据指定的用户账号与密码，查询比对用户的信息是否符合。返回用户详细信息。
	 * 
	 * @param sysUser
	 *            指定的用户账户名。
	 */
	public SysUser validateUser(SysUser sysUser) {
		LOGGER.debug("用户登录");

		// 将用户输入的密码MD5加密
		sysUser.setPassword(EncrptUtil.encryptMD5((sysUser.getPassword())));

		if (StringUtil.isEmpty(sysUser.getAccount())) {
			LOGGER.warn("登录用户名为空");
			return null;
		}

		SysUser loginUser = sysUserMapper.queryLogin(sysUser.getAccount());
		if (loginUser == null) {
			return null;
		}
		if (sysUser.getPassword().equals(loginUser.getPassword())) {
			LOGGER.debug("登入成功");
			return loginUser;
		} else {
			return null;
		}
	}

	/**
	 * 由业务类实现
	 *
	 * @return
	 */
	@Override
	public BaseMapper<SysUser> getBaseMapper() {
		return sysUserMapper;
	}
}